package com.quectel.cms.controller.sys;


import com.quectel.cms.controller.BaseController;
import com.quectel.cms.util.SessionHolder;
import com.quectel.util.aspect.log.annotation.Log;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.sys.module.sys.dto.SysRoleDto;
import com.quectel.sys.module.sys.service.SysRoleService;
import com.quectel.sys.module.sys.service.SysUserService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2019-04-01 22:52:09
 */
@RestController
@RequestMapping("sysRole")
@Tag(name = "SysRoleController", description = "系统角色相关api")
public class SysRoleController extends BaseController {

    @DubboReference
    private SysRoleService sysRoleService;
    @DubboReference
    private SysUserService sysUserService;

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "sysRole:list")
    @Operation(summary = "系统用户分页列表查询")
    public Response<Page<SysRoleDto>> list(@RequestParam Map<String, Object> params) {
        super.fillAuthorityData(params);

        Request request = Request.configParams(params).like("name");
        List<SysRoleDto> list = new ArrayList<>();

        int total = sysRoleService.queryTotal(params);
        if (total > 0) {
            list = sysRoleService.queryList(params);
        }
        Page<SysRoleDto> page = new Page(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<SysRoleDto>>ok().wrap(page);
    }


    /**
     * 列表
     */
    @GetMapping("queryAllRoles")
    @Operation(summary = "根据参数查询所有的角色列表")
    public Response<List<SysRoleDto>> queryAllRoles() {
        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        List<SysRoleDto> sysRoleDtos = sysRoleService.queryList(params);
        return Response.<List<SysRoleDto>>ok().wrap(sysRoleDtos);
    }

    /**
     * 信息
     */
    @RequestMapping("info/{id}")
    @RequirePermissions(values = "sysRole:info")
    @Operation(summary = "根据id查询系统角色记录")
    public Response<SysRoleDto> info(@PathVariable("id") Long id) {
        SysRoleDto sysRoleDto = sysRoleService.selectById(id);

        return Response.<SysRoleDto>ok().wrap(sysRoleDto);
    }

    /**
     * 保存
     */
    @RequestMapping("save")
    @RequirePermissions(values = "sysRole:edit")
    @Log(express = "保存角色")
    @Operation(summary = "保存系统角色记录")
    public Response<Object> save(@RequestBody @Validated SysRoleDto sysRoleDto) {

        SysRoleDto dbRole = sysRoleService.selectByTenantIdAndRoleName(SessionHolder.getSysUser().getTenantId(), sysRoleDto.getName());
        if (dbRole != null) {
            return Response.error("该角色已经存在");
        }
        fillSaveCommonData(sysRoleDto);

        sysRoleDto.setGroupId(SessionHolder.getSysUser().getGroupId());
        sysRoleService.save(sysRoleDto);

        return Response.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("update")
    @RequirePermissions(values = "sysRole:edit")
    @Log(express = "修改角色")
    @Operation(summary = "修改系统角色记录")
    public Response<Object> update(@RequestBody @Validated SysRoleDto sysRoleDto) {

        SysRoleDto dbRole = sysRoleService.selectByTenantIdAndRoleName(SessionHolder.getSysUser().getTenantId(), sysRoleDto.getName());
        if (dbRole != null && !Objects.equals(dbRole.getId(), sysRoleDto.getId())) {
            return Response.error("您修改的角色已经存在");
        }

        fillEditCommonData(sysRoleDto);

        sysRoleService.updateById(sysRoleDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("delete")
    @RequirePermissions(values = "sysRole:delete")
    @Log(express = "删除角色")
    @Operation(summary = "删除系统角色记录")
    public Response<Object> delete(@RequestBody Long[] ids) {
        for (Long id : ids) {
            SysRoleDto sysRoleDto = sysRoleService.selectById(id);
            if (Objects.equals(sysRoleDto.getName(), "Super")) {
                return Response.error("超级管理员角色不能被删除");
            }
        }
        sysRoleService.deleteBatch(ids);
        return Response.ok();
    }
}
